package com.example.administrator.szb.activity.teseservice;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.Success2Activity;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseActivity;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.CheckInfoUtils;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.SPUtils;
import com.example.administrator.szb.view.TimePickUtils2;

import java.util.HashMap;
import java.util.Map;

public class TeseFabuActivity extends MVPBaseActivity {
    private EditText et_title;
    private LinearLayout ll_time;
    private TextView tv_time,bar_title;
    private EditText tv_content;
    private Button send_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tese_fabu);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        bar_title = findViewById(R.id.bar_title);
        bar_title.setText("发布服务");
        et_title = (EditText) findViewById(R.id.et_title);
        ll_time = (LinearLayout) findViewById(R.id.ll_time);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_content = (EditText) findViewById(R.id.tv_content);
        send_btn = (Button) findViewById(R.id.send_btn);
    }

    @Override
    protected void initEvent() {
        ll_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TimePickUtils2(context, new TimePickUtils2.OnTimeSelectListener() {
                    @Override
                    public void onSelect(String time) {
                        tv_time.setText(time);
                    }
                });

            }
        });

        send_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckInfoUtils.check(activity, iosDiaolog, send_btn, new CheckInfoUtils.OnSuccessLietener() {
                    @Override
                    public void onSuccess() {
                        if (canDO()){
                            fabu();
                        }
                    }
                });
            }
        });
    }

    @Override
    protected void initData() {

    }

    private boolean canDO(){
        boolean can = true;
        String str = "";
        if(TextUtils.isEmpty(et_title.getText().toString().trim())){
            can=false;
            str="请输入标题";
        }else if (TextUtils.isEmpty(tv_time.getText().toString().trim())){

            can=false;
            str="请选择时间";
        }else if (TextUtils.isEmpty(tv_content.getText().toString().trim())){
            can=false;
            str="请输入内容";
        }
        if (!can){
            DialogUtil.showToast(context,str);
        }
        return can;
    }
    private Map<String,Object> getMap(){
        Map<String,Object> map = new HashMap<>();
        map.put("uid", SPUtils.getUserId());
        map.put("title",et_title.getText().toString().trim());
        map.put("content",tv_content.getText().toString().trim());
        if (tv_time.getText().toString().contains("限")){

        }else {
            map.put("end_time",tv_time.getText().toString());
        }

        return map;
    }
    private void fabu(){
        send_btn.setEnabled(false);
        DialogUtil.showIsoProgressbar(context,"发布中");
        HttpUtils.post(activity,URLAddress.SPECIAL_ADD, getMap(), new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                DialogUtil.dialog_ios.dismiss();
                send_btn.setEnabled(true);
                DialogUtil.showToast(context,str);
                Intent intent = new Intent(context, Success2Activity.class);
                intent.putExtra("titles","发布成功");
                intent.putExtra("content","特色服务发布成功，客服经理将在24小时内与您联系");
                startActivity(intent);
                finish();
            }

            @Override
            public void onFailure(int requestCode, String str) {
                DialogUtil.dialog_ios.dismiss();
                send_btn.setEnabled(true);
            }
        });
    }
}
